import { StrokePoint, Point } from '../geometry/point';
import { Rect } from '../geometry/rect';
import { InkSettings } from './inksetting';

export class Stroke {
    private _minX = 0;
    private _minY = 0;
    private _maxX = 0;
    private _maxY = 0;
    public strokeId: string;
    public strokeWidth: number;
    public strokeColor: string = InkSettings.DefaultStrokeColor;
    private _points: StrokePoint[] = [];

    /**
     * 构造函数
     * @param strokeId 线条标识
     * @param strokeWidth 线条宽度
     * @param strokeColor 线条颜色
     */
    constructor(strokeId: string, strokeWidth: number = 2, strokeColor: string = InkSettings.DefaultStrokeColor) {
        this.strokeId = strokeId;
        this.strokeWidth = strokeWidth;
        this.strokeColor = strokeColor;
    }
    public get points() {
        return this._points;
    }
    /**
     * 添加点到线条
     * @param x 点x坐标
     * @param y 点y坐标
     * @param factor 压感因子值
     */
    public addPoint(x: number, y: number, factor: number = 0.5) {
        this.points.push(new StrokePoint(x, y, factor));
        if ( this.points.length === 1) {
            this._minX = this._maxX = x;
            this._minY = this._maxY = y;
        } else {
            this._minX = Math.min(this._minX, x);
            this._minY = Math.min(this._minY, y);
            this._maxX = Math.max(this._maxX, x);
            this._maxY = Math.max(this._maxY, y);
        }
    }
    /**
     * 添加点到线条
     * @param point 坐标点
     */
    public addPoint2(point: StrokePoint) {
        this.addPoint(point.x, point.y, point.factor);
    }
    /**
     * 添加一系列坐标点到线条
     * @param points 要添加的点集合
     */
    public addPoints(points: StrokePoint[]) {
        if (points && points.length > 0) {
            points.forEach((p) => this.addPoint(p.x, p.y, p.factor));
        }
    }
    public replacePoints(points: StrokePoint[]) {
        this._points = [];
        this.addPoints(points);
    }
    /**
     * 获取线条的呈现范围
     */
    public get bounds(): Rect {
        return new Rect(this._minX, this._minY, Math.abs(this._maxX - this._minX), Math.abs(this._maxY - this._minY));
    }
}